package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * spring容器启动监听器, 拥有初始化redis数据
 */


@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent>{


    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisVOService strategyStatisRedisService;

    @Reference
    private IStrategyStatisVOService statisVOService;

    //spring容器启动之后马山执行方法
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("------------vo数据初始化-start--------------------------");
        //从mysql中获取所有的攻略明细数据
        List<StrategyDetail> list = strategyDetailService.list();
        StrategyStatisVO vo = null;
       for (StrategyDetail detail : list) {
            //存在一个小细节: 如果redis已经存在该vo数据, 需要跳过, 否则会出现数据丢失情况
            if(strategyStatisRedisService.isStrategyStatisVOExsit(detail.getId())){
                continue;  //跳过此次循环
            }
            vo = new StrategyStatisVO();
            //遍历添加到redis中
            try {
                //强调: 属性名一致时,才可以进行copy
                BeanUtils.copyProperties(vo, detail);//底层使用javabean内省机制
                vo.setDestId(detail.getDest().getId());
                vo.setDestName(detail.getDest().getName());
                vo.setStrategyId(detail.getId());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            //设置
            strategyStatisRedisService.setStrategyStatisVO(vo);
        }

        System.out.println("------------end--------------------------");



        System.out.println("------------排行srart--------------------------");
        //热门排行
        for (StrategyDetail detail : list) {
            //存放vo对象的key
            String voKey = RedisKeys.STRATEGY_STATIS_VO.Join(detail.getId().toString());
            //拍行的key
            String zsetKey = RedisKeys.STRATEGY_HOT_ZSET.getPrefix();
            //判断排行key的 value是否存在vokey
            if (statisVOService.isExsitZset(zsetKey,voKey)){
                continue;
            }
            int score=detail.getViewnum()+detail.getReplynum();
            statisVOService.addHotScore(detail.getId(),score);

        }
        //国内外排行
        for (StrategyDetail detail : list) {
            //存放vo对象的key
            String voKey = RedisKeys.STRATEGY_STATIS_VO.Join(detail.getId().toString());
            //拍行的key
            String zsetKey =null;
            if (detail.isIsabroad()){
                zsetKey=RedisKeys.STRATEGY_ABROADCDS_ZSET.getPrefix();
            }else {
                zsetKey=RedisKeys.STRATEGY_CHINA_ZSET.getPrefix();
            }
            //判断排行key的 value是否存在vokey
            if (statisVOService.isExsitZset(zsetKey,voKey)){
                continue;
            }
            int score=detail.getThumbsupnum()+detail.getFavornum();
            statisVOService.addCommendScores(detail.getId(),score);

        }
        System.out.println("------------排行end--------------------------");
    }
}
